﻿<%@ WebHandler Language="C#" Class="jsonPager" %>

using System;
using System.Web;
using System.Text;
using System.Collections.Generic;

public class jsonPager : IHttpHandler
{
    public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "text/plain";
        string temp = "Hello World";
        if (!string.IsNullOrEmpty(context.Request["type"]))
        {
            string type = context.Request["type"];
            if ("pager" == type)//分页条
            {
                temp = GetPager(context);
            }
            else if ("json" == type)//列表json数据
            {
                temp = GetJson(context);
            }
            else if ("delete" == type)
            {
                temp = Delete(context);
            }
            else if ("deleteSelect" == type)
            {
                temp = DeleteSelect(context); 
            }
        }

        context.Response.Write(temp);
    }

    /// <summary>
    /// 获取页码
    /// </summary>
    /// <returns></returns>
    public string GetPager(HttpContext context)
    {
        StringBuilder sb = new StringBuilder("<span class='disabled'>上一页</span>");
        sb.Append("<span class='current'>1</span>");
        sb.Append("<a href='#2' class='current'>2</a>");
        sb.Append("<a href='#3' class='current'>3</a>");
        sb.Append("<a href='#4' class='current'>4</a>");
        sb.Append("<a href='#5' class='current'>5</a>");
        sb.Append("<a href='#6' class='current'>6</a>");
        sb.Append("<span>...<span>");
        sb.Append("<a href='#85' class='current'>85</a>");
        sb.Append("<a href='#86' class='current'>86</a>");
        sb.Append("<a href='#2' class='disabled'>下一页</a>");
        return sb.ToString();
    }

    /// <summary>
    /// 获取json数据
    /// </summary>
    /// <param name="context"></param>
    /// <returns></returns>
    public string GetJson(HttpContext context)
    {
        if (!string.IsNullOrEmpty(context.Request["page"]))
        {
            return DB.GetFriendLinkList(int.Parse(context.Request["page"]), 20).ToJson();
        }
        else
        {
            return "Hello World";
        }
    }

    /// <summary>
    /// 删除数据
    /// </summary>
    /// <param name="context"></param>
    /// <returns></returns>
    public string Delete(HttpContext context)
    {
        if (!string.IsNullOrEmpty(context.Request["linkID"]))
        {
            if (DB.Delete(int.Parse(context.Request["linkID"])))
                return "success";
            return "Hello World";
        }
        else
        {
            return "Hello World";
        }
    }

    /// <summary>
    /// 删除多个数据
    /// </summary>
    /// <param name="context"></param>
    /// <returns></returns>
    public string DeleteSelect(HttpContext context)
    {
        if (!string.IsNullOrEmpty(context.Request.QueryString["linkList"]))
        {
            string strlink = context.Request.QueryString["linkList"];
            string[] arrlink = strlink.Substring(1, strlink.Length - 1).Split('-');
            IList<string> list=new List<string>(arrlink);
            if (DB.Delete(list))
            {
                return "success";
            }
            else
            {
                return "Hello World";
            }
        }
        else
        {
            return "Hello World";
        }
    }


    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

}